package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass=MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
  @Autowired
  private MemberDao memberDao;


    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void addMember(Member member) {
        String password = member.getPassword();
        //判断密码是否为空
        if(password!=null){
            //如果不为空 通过工具类给密码进行加密
            String md5password = MD5Utils.md5(password);
            member.setPassword(md5password);
        }
        memberDao.add(member);
    }


    //根据月份查询会员数量
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
         String date=month+"%";
            Integer count = memberDao.findMemberCountByMonths(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public List<Integer> findAppointmentsByMonths(List<String> months) {
        List<Integer> appointments = new ArrayList<>();
        for (String month : months) {
            String date=month+"%";
            Integer count = memberDao.findAppointmentsByMonths(date);
            appointments.add(count);
        }
        return appointments;
    }

    @Override
    public List<Integer> findConsultationsByMonths(List<String> months) {
        List<Integer> consultations = new ArrayList<>();
        for (String month : months) {
            String date=month+"%";
            Integer count = memberDao.findConsultationsByMonths(date);
            consultations.add(count);
        }
        return consultations;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取一页几行
        Integer pageSize = queryPageBean.getPageSize();
        //获取当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取查找的字符串
        String queryString = queryPageBean.getQueryString();
        //利用分页助手
        PageHelper.startPage(currentPage,pageSize);
        //调用dao层方法
        Page<Member> page = memberDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Member> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void deleteById(Integer id) {
        //判断当前id是否被加入会员中
        long count = memberDao.deleteById(id);
        if (count>0){
            //当前id被加入会员中,不能被删除
            new RuntimeException();
        }else{
            //当前id可以删除
            memberDao.deleteById(id);
        }
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    @Override
    public Map<String, Object> getexportReportExcel() {
        return null;
    }
}
